//
//  XQHomeSmallCardCell.m
//  JoinPeso
//
//  Created by 小琦 on 2024/11/20.
//

#import "XQHomeSmallCardCell.h"
#import "XQHomeSmallCardUpAndDownView.h"

@interface XQHomeSmallCardCell()
@property (nonatomic,strong) UIView *bgView;
@property (nonatomic,strong) UIImageView *logoImageView;
@property (nonatomic,strong) UILabel *titleLb;
@property (nonatomic,strong) UIButton *buyButton;
@property (nonatomic,strong) UILabel *dollorLb;
@property (nonatomic,strong) UILabel *priceLb;
@property (nonatomic,strong) XQHomeSmallCardUpAndDownView *termView;
@property (nonatomic,strong) XQHomeSmallCardUpAndDownView *rateView;
@end

@implementation XQHomeSmallCardCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if(self) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        self.backgroundColor = [UIColor clearColor];
        [self makeUI];
        [self makeConstraints];
    }
    return self;
}

- (void)makeUI {
    [self.contentView addSubview:self.bgView];
    [self.bgView addSubview:self.logoImageView];
    [self.bgView addSubview:self.titleLb];
    [self.bgView addSubview:self.buyButton];
    [self.bgView addSubview:self.dollorLb];
    [self.bgView addSubview:self.priceLb];
    [self.bgView addSubview:self.termView];
    [self.bgView addSubview:self.rateView];
}

- (void)makeConstraints {
    
    [self.bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(@0);
        make.left.equalTo(@16);
        make.right.equalTo(@-16);
        make.bottom.equalTo(@-16);
    }];
    
    [self.logoImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(@14);
        make.left.equalTo(@16);
        make.width.height.equalTo(@22);
    }];
    
    [self.titleLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.logoImageView);
        make.left.equalTo(self.logoImageView.mas_right).offset(4);
    }];
    
    [self.buyButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.logoImageView);
        make.right.equalTo(@-16);
        make.width.equalTo(@90);
        make.height.equalTo(@26);
    }];
    
    [self.priceLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.logoImageView.mas_bottom).offset(13);
        make.left.equalTo(@29);
    }];
    
    [self.dollorLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.priceLb);
        make.right.equalTo(self.priceLb.mas_left).offset(-2);
    }];
    
    [self.termView mas_makeConstraints:^(MASConstraintMaker *make) {
        (void)make.centerX;
        make.bottom.equalTo(@-12);
    }];
    
    [self.rateView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(@-26);
        make.bottom.equalTo(@-12);
    }];
}

#pragma mark - setter

- (void)setContentDic:(NSDictionary *)contentDic {
    [self.logoImageView sd_setImageWithURL:[NSURL URLWithString:contentDic[@"networker"]] placeholderImage:[UIImage imageNamed:@"home_small_card_logo"]];
    self.titleLb.text = contentDic[@"airborne"];
    [self.buyButton setTitle:contentDic[@"hughes"] forState:UIControlStateNormal];
    
    NSString *price = contentDic[@"china"];
    if ([price containsString:@" "]) {
        NSArray *priceArr = [price componentsSeparatedByString:@" "];
        if (priceArr.count == 2 ) {
            self.dollorLb.text = priceArr.firstObject;
            self.priceLb.text = [XQUnitChange unitChange:priceArr.lastObject];
        }
    }

    self.termView.upLb.text = contentDic[@"articles"];
    self.termView.downLb.text = contentDic[@"conflicts"];
      
    self.rateView.upLb.text = contentDic[@"encountered"];
    self.rateView.downLb.text = contentDic[@"newark"];
}

#pragma mark - lazy

- (UIView *)bgView {
    if (!_bgView) {
        _bgView = [[UIView alloc] init];
        _bgView.backgroundColor = [UIColor whiteColor];
        _bgView.layer.cornerRadius = 4;
    }
    return _bgView;
}

- (UIImageView *)logoImageView {
    if (!_logoImageView) {
        _logoImageView = [[UIImageView alloc] init];
       
    }
    return  _logoImageView;
}

- (UILabel *)titleLb {
    if (!_titleLb) {
        _titleLb = [[UILabel alloc] init];
        _titleLb.textColor = kBlackTextColor;
        _titleLb.font = [UIFont boldSystemFontOfSize:16];
    }
    return  _titleLb;
}

- (UIButton *)buyButton {
    if (!_buyButton) {
        _buyButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _buyButton.backgroundColor = kBlueTextColor;
        [_buyButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        _buyButton.titleLabel.font = [UIFont boldSystemFontOfSize:14];
        _buyButton.layer.cornerRadius = 13;
        [_buyButton setUserInteractionEnabled:NO];
    }
    return _buyButton;
}

- (UILabel *)dollorLb {
    if (!_dollorLb) {
        _dollorLb = [[UILabel alloc] init];
        _dollorLb.textColor = kBlackTextColor;
        _dollorLb.font = [UIFont systemFontOfSize:16];
    }
    return  _dollorLb;
}

- (UILabel *)priceLb {
    if (!_priceLb) {
        _priceLb = [[UILabel alloc] init];
        _priceLb.textColor = kBlackTextColor;
        _priceLb.font = [UIFont boldSystemFontOfSize:28];
    }
    return  _priceLb;
}

- (XQHomeSmallCardUpAndDownView *)termView {
    if (!_termView) {
        _termView = [[XQHomeSmallCardUpAndDownView alloc] init];
        _termView.upLb.textAlignment = NSTextAlignmentCenter;
        _termView.downLb.textAlignment = NSTextAlignmentCenter;
    }
    return _termView;
}

- (XQHomeSmallCardUpAndDownView *)rateView {
    if (!_rateView) {
        _rateView = [[XQHomeSmallCardUpAndDownView alloc] init];
        _rateView.upLb.textAlignment = NSTextAlignmentCenter;
        _rateView.downLb.textAlignment = NSTextAlignmentCenter;
    }
    return  _rateView;
}


@end
